package com.ruoyi.web.controller.biz;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsJingquJingdian;
import com.ruoyi.system.domain.dto.BsJingquJingdianDto;
import com.ruoyi.system.domain.vo.BsJingquJingdianVo;
import com.ruoyi.system.service.BsJingquJingdianService;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BsJingquJingDianController
 * @Description 描述
 * @Author HanWL
 * @Date 2023/10/16 22:49
 * @Version 1.0
 **/
@RestController
@Api(tags = {"电子讲解", "景区景点接口"})
@RequestMapping("/bs/jingqu/jingdian")
public class BsJingquJingDianController extends BaseController {

    @Resource
    private BsJingquJingdianService bsJingquJingdianService;

    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    public Result<TableDataInfo<BsJingquJingdianDto>> pageList() {
        PageDomain pageDomain = TableSupport.getPageDomain();
        return Result.success(bsJingquJingdianService.pageList(pageDomain));
    }
    
    @PostMapping("/save")
    @ApiOperation("新增")
    public Result<?> save(@Validated(value = {ValidInsertGroup.class}) @RequestBody BsJingquJingdianVo jingquJingdianVo) throws Exception {
        boolean save = bsJingquJingdianService.saveEntity(jingquJingdianVo);
        return save ? Result.success("新增成功") : Result.error("新增失败");
    }

    @PutMapping("/update")
    @ApiOperation("编辑")
    public Result<?> update(@Validated(value = {ValidUpdateGroup.class}) @RequestBody BsJingquJingdianVo jingquJingdianVo) throws Exception {
        boolean update = bsJingquJingdianService.updateEntity(jingquJingdianVo);
        return update ? Result.success("编辑成功") : Result.error("编辑失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public Result<?> delete(@ApiParam("景区id") @PathVariable("id") Long id) throws Exception {
        boolean delete = bsJingquJingdianService.delete(id);
        return delete ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/getJingDianByJingQuId/{JingQuId}")
    @ApiOperation(value = "根据景区id获取下面所有电子讲解", tags = {"根据景区id获取下面所有电子讲解", "根据景区的ID获取下面的所有景点"})
    public AjaxResult<List<BsJingquJingdian>> getJingDianByJingQuId(@PathVariable Long JingQuId){
        LambdaQueryWrapper<BsJingquJingdian> lqw = new LambdaQueryWrapper<BsJingquJingdian>()
                .eq(BsJingquJingdian::getJingquId, JingQuId);
        List<BsJingquJingdian> list = bsJingquJingdianService.list(lqw);
        if (CollUtil.isNotEmpty(list)) {
            list.parallelStream().forEach(i->i.setDurationInfo(DateUtils.getTimePoor(i.getDuration())));
        }
        return AjaxResult.success(list);
    }

}
